<%--
  Created by IntelliJ IDEA.
  User: 16657
  Date: 2018/10/28
  Time: 13:12
  To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>SSE Demo</title>
</head>
<body>
<div id="msgFrompPush"></div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath}/assets/js/jquery-1.10.2.js"></script>
<script>
    if (!!window.EventSource) {//EventSource对象只有新式的浏览器才有（Chrome,Firefox）等，EventSource是SSE的客户端
        var source = new EventSource('push');
        var s = "";
        source.addEventListener('message', function (e) { //添加SSE的客户端监听，在此获得服务器端推送的信息
            s += e.data + "<br/>";
            $("#msgFrompPush").html(s);
        });
        source.addEventListener('open', function (e) {
            console.log("连接打开");
        }, false);
        source.addEventListener('error', function (e) {
            if (e.readyState == EventSource.ClOSED) {
                console.log("关闭连接");
            } else {
                console.log(e.readyState);
            }
        }, false);
    } else {
        console.log("你的浏览器不支持SSE")
    }
</script>
</html>
